package com.fsnbigdata.domain;

import com.fsnbigdata.util.ChineseInital;
import io.springlets.format.EntityFormat;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.annotations.Index;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.roo.addon.javabean.annotations.RooEquals;
import org.springframework.roo.addon.javabean.annotations.RooJavaBean;
import org.springframework.roo.addon.javabean.annotations.RooToString;
import org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaEntity;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaRelation;
import org.springframework.util.Assert;

/**
 * = Specimen
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJavaBean
@RooToString
@RooJpaEntity(entityFormatExpression = "#{collectNumber}")
@RooEquals(isJpaEntity = true)
@Entity
@EntityFormat("#{collectNumber}")
public class Specimen {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Version
    private Integer version;

    /**
     * 名称
     */
    @NotNull
    @Length(min = 1, max = 255,message ="名称信息内容长度必须在1-255个字符之间")
    private String name;

    /**
     * 物种拉丁名
     */
    @NotNull
    @Length(min = 1, max = 255,message ="物种拉丁名信息内容长度必须在1-255个字符之间")
    private String latinName;

    /**
     * 拼音首字母
     */
    @Index(name="Specimen_initial")
    private String initial;

    /**
     * 采集号
     */
    @Length(min = 0, max = 255,message ="采集号信息内容长度必须在0-255个字符之间")
    private String collectNumber;

    /**
     * 采集地
     */
    @Length(min = 0, max = 255,message ="采集地信息内容长度必须在0-255个字符之间")
    private String collectLocation;

    /**
     * 采集时间
     */
    @Length(min = 0, max = 255,message ="采集时间信息内容长度必须在0-255个字符之间")
    private String collectDate;

    /**
     * 采集人
     */
    @Length(min = 0, max = 255,message ="采集人信息内容长度必须在0-255个字符之间")
    private String collectPerson;

    /**
     * 采集地坐标
     */
    @Length(min = 0, max = 255,message ="采集地坐标信息内容长度必须在0-255个字符之间")
    private String collectCoordinate;

    /**
     * 标本情况
     */
    @Length(min = 0, max = 255,message ="标本情况信息内容长度必须在0-255个字符之间")
    private String status;

    /**
     * 标本描述
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Length(min = 0, max = 1000,message ="标本描述信息内容长度必须在0-1000个字符之间")
    private String description;

    /**
     * 保存地
     */
    @Length(min = 0, max = 255,message ="保存地信息内容长度必须在0-255个字符之间")
    private String preserveLocation;

    /**
     * 特有性
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String endemism;

    /**
     * 分布信息
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Length(min = 0, max = 800,message ="分布信息内容长度必须在0-800个字符之间")
    private String distribution;

    /**
     * 生物id
     */
    @NumberFormat
    private Long organismId;

    /**
     * 创建者
     */
    private String creator;

    /**
     * 类型
     */
    @Range(min = 1,max = 2,message = "请正确选择相应类型")
    private Integer type;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "specimen", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<SpecimenDocument> documents = new HashSet<SpecimenDocument>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @ManyToMany(cascade = { javax.persistence.CascadeType.REFRESH, javax.persistence.CascadeType.PERSIST }, fetch = FetchType.EAGER, mappedBy = "specimens")
    @RooJpaRelation(type = JpaRelationType.AGGREGATION)
    @Valid
    private Set<Document> docs = new HashSet<Document>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "specimen")
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<SpecimenImage> images = new HashSet<SpecimenImage>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "specimen", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<SpecimenVideo> videos = new HashSet<SpecimenVideo>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * This `equals` implementation is specific for JPA entities and uses
     * the entity identifier for it, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @param obj
     * @return Boolean
     */
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        // instanceof is false if the instance is null
        if (!(obj instanceof Specimen)) {
            return false;
        }
        return getId() != null && Objects.equals(getId(), ((Specimen) obj).getId());
    }

    /**
     * This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able
     * to identify the entity in collections after a new id is assigned to the entity, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @return Integer
     */
    public int hashCode() {
        return 31;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getId() {
        return this.id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Integer
     */
    public Integer getVersion() {
        return this.version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param version
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getName() {
        return this.name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getLatinName() {
        return this.latinName;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param latinName
     */
    public void setLatinName(String latinName) {
        this.latinName = latinName;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getInitial() {
        return this.initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param initial
     */
    public void setInitial(String initial) {
        this.initial = initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCollectNumber() {
        return this.collectNumber;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param collectNumber
     */
    public void setCollectNumber(String collectNumber) {
        this.collectNumber = collectNumber;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCollectLocation() {
        return this.collectLocation;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param collectLocation
     */
    public void setCollectLocation(String collectLocation) {
        this.collectLocation = collectLocation;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCollectDate() {
        return this.collectDate;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param collectDate
     */
    public void setCollectDate(String collectDate) {
        this.collectDate = collectDate;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCollectPerson() {
        return this.collectPerson;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param collectPerson
     */
    public void setCollectPerson(String collectPerson) {
        this.collectPerson = collectPerson;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCollectCoordinate() {
        return this.collectCoordinate;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param collectCoordinate
     */
    public void setCollectCoordinate(String collectCoordinate) {
        this.collectCoordinate = collectCoordinate;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getStatus() {
        return this.status;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param status
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getDescription() {
        return this.description;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getPreserveLocation() {
        return this.preserveLocation;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param preserveLocation
     */
    public void setPreserveLocation(String preserveLocation) {
        this.preserveLocation = preserveLocation;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getEndemism() {
        return this.endemism;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param endemism
     */
    public void setEndemism(String endemism) {
        this.endemism = endemism;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getDistribution() {
        return this.distribution;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param distribution
     */
    public void setDistribution(String distribution) {
        this.distribution = distribution;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getOrganismId() {
        return this.organismId;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organismId
     */
    public void setOrganismId(Long organismId) {
        this.organismId = organismId;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCreator() {
        return this.creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param creator
     */
    public void setCreator(String creator) {
        this.creator = creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<SpecimenDocument> getDocuments() {
        return this.documents;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param documents
     */
    public void setDocuments(Set<SpecimenDocument> documents) {
        this.documents = documents;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<Document> getDocs() {
        return this.docs;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param docs
     */
    public void setDocs(Set<Document> docs) {
        this.docs = docs;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<SpecimenImage> getImages() {
        return this.images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param images
     */
    public void setImages(Set<SpecimenImage> images) {
        this.images = images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<SpecimenVideo> getVideos() {
        return this.videos;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param videos
     */
    public void setVideos(Set<SpecimenVideo> videos) {
        this.videos = videos;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    @Override
    public String toString() {
        return "Specimen{" +
                "id=" + id +
                ", version=" + version +
                ", name='" + name + '\'' +
                ", latinName='" + latinName + '\'' +
                ", initial='" + initial + '\'' +
                ", collectNumber='" + collectNumber + '\'' +
                ", collectLocation='" + collectLocation + '\'' +
                ", collectDate='" + collectDate + '\'' +
                ", collectPerson='" + collectPerson + '\'' +
                ", collectCoordinate='" + collectCoordinate + '\'' +
                ", status='" + status + '\'' +
                ", description='" + description + '\'' +
                ", preserveLocation='" + preserveLocation + '\'' +
                ", endemism='" + endemism + '\'' +
                ", distribution='" + distribution + '\'' +
                ", organismId=" + organismId +
                ", creator='" + creator + '\'' +
                ", type=" + type +
                ", documents=" + documents +
                ", docs=" + docs +
                ", images=" + images +
                ", videos=" + videos +
                '}';
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param documentsToAdd
     */
    public void addToDocuments(Iterable<SpecimenDocument> documentsToAdd) {
        Assert.notNull(documentsToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (SpecimenDocument item : documentsToAdd) {
            this.documents.add(item);
            item.setSpecimen(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param documentsToRemove
     */
    public void removeFromDocuments(Iterable<SpecimenDocument> documentsToRemove) {
        Assert.notNull(documentsToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (SpecimenDocument item : documentsToRemove) {
            this.documents.remove(item);
            item.setSpecimen(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param docsToAdd
     */
    public void addToDocs(Iterable<Document> docsToAdd) {
        Assert.notNull(docsToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (Document item : docsToAdd) {
            this.docs.add(item);
            item.getSpecimens().add(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param docsToRemove
     */
    public void removeFromDocs(Iterable<Document> docsToRemove) {
        Assert.notNull(docsToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (Document item : docsToRemove) {
            this.docs.remove(item);
            item.getSpecimens().remove(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToAdd
     */
    public void addToImages(Iterable<SpecimenImage> imagesToAdd) {
        Assert.notNull(imagesToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (SpecimenImage item : imagesToAdd) {
            this.images.add(item);
            item.setSpecimen(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToRemove
     */
    public void removeFromImages(Iterable<SpecimenImage> imagesToRemove) {
        Assert.notNull(imagesToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (SpecimenImage item : imagesToRemove) {
            this.images.remove(item);
            item.setSpecimen(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param videosToAdd
     */
    public void addToVideos(Iterable<SpecimenVideo> videosToAdd) {
        Assert.notNull(videosToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (SpecimenVideo item : videosToAdd) {
            this.videos.add(item);
            item.setSpecimen(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param videosToRemove
     */
    public void removeFromVideos(Iterable<SpecimenVideo> videosToRemove) {
        Assert.notNull(videosToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (SpecimenVideo item : videosToRemove) {
            this.videos.remove(item);
            item.setSpecimen(null);
        }
    }

    @PrePersist
    public void prePersist(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }

    @PreUpdate
    public void preUpdate(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }
}
